package com.atxiaodei.kabudarpc.handler;

import com.atxiaodei.kabudarpc.annotion.KabudaRemoteService;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

/**
 * @ClassName InitialMerdiator
 * @Deacription 通过Spring Bean后置通知，找到被自定义注解修饰的类，存到Merdiator类的map
 * @Author 卡布达
 * @Date 2021/10/31 16:08
 * @Version 1.0
 **/
@Configuration
public class InitialMerdiator implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        if(bean.getClass().isAnnotationPresent(KabudaRemoteService.class)){
            Method[] declaredMethods = bean.getClass().getDeclaredMethods();
            for (Method method : declaredMethods) {
//                接口类名+方法名称  方便找到实例
                String key=bean.getClass().getInterfaces()[0].getName()+"."+method.getName();
                BeanMethod beanMethod=new BeanMethod();
                beanMethod.setBean(bean);
                beanMethod.setMethod(method);
                Mediator.map.put(key,beanMethod);
            }
        }
        return bean;
    }
}
